<%-- 
    Document   : queryInfo
    Created on : 2017-3-24, 10:15:04
    Author     : 52461
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="../../link.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="${base}/js/highcharts/highcharts.js" ></script>
        <script type="text/javascript" src="${base}/js/mychartoption.js" ></script>
        <script type="text/javascript" src="${base}/js/jquery-migrate-1.0.0.js" ></script>
        <script type="text/javascript" src="${base}/js/jqprint.js" ></script>
        <title>质控管理</title>
        <style>
            .form-control{
                width: 100px;
            }
            .box-shadow-8{  
                box-shadow:0 0 12px 2px #888888,  
                    0 0 12px 5px #888888;  
            } 
        </style>
    </head>
    <body ng-app="zkManagerApp" ng-controller="mainCtrl" style="overflow: hidden;margin-top: 5px;">

        <div class="col-lg-12">
            <div class="panel panel-default" style="overflow: hidden">

                <div class="panel-heading head-width"><span style="float: left;line-height: 30px;font-size: 16px; font-weight: bold;margin-right: 10px;">质控管理</span>
                    <div class="form-inline">

                        <div class="form-group">
                            <label for="startTime">设备:</label>
                            <select ng-model="device" ng-change="getPh()" style="width:120px" class="form-control" id="startTime">
                                <option ng-repeat="de in des" value="{{de.DEVICEMODEL}}">{{de.DEVICEMODEL}}</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="startTime">试剂批号:</label>
                            <select ng-model="ph" ng-init="ph = phs[0]"  ng-change="queryDeviceStandard()" ng-options="x.BATCH_NUMBER for x in phs" style="width:120px" class="form-control" >

                            </select>
                        </div>
                        <div class="form-group">
                            <label for="startTime">年:</label>
                            <input type="number" ng-model="query_year" style="width: 80px"  class="form-control" />
                        </div>
                        <div class="form-group">
                            <label for="startTime">月:</label>
                            <input type="number" ng-model="query_month" style="width: 80px" class="form-control" />
                        </div>
                        <button ng-disabled="!items" ng-click="queryByBtn()" class="btn btn-success "><span class="fa fa-search"></span>&nbsp;&nbsp;&nbsp;查询</button>

                        <button ng-disabled="!results" ng-click="printChart()" class="btn btn-success "><span class="fa fa-print"></span>&nbsp;&nbsp;&nbsp;打印质控图</button>

                        <!--<button ng-click="printResult()" class="btn btn-success btn-sm"><span class="fa fa-print"></span>&nbsp;&nbsp;&nbsp;打印</button>-->
                    </div>


                    <div style="clear: both"></div>
                </div>  
                <table  style="width:100%">


                    <tr>
                        <td  class="main-left" valign="top" style="overflow: auto;width: 320px">
                            <div class=" main main-left" style="overflow: auto;width: 300px"  >
                                <table class="table table-bordered ">
                                    <tr>
                                        <th style="width:50px;">序号</th>
                                        <th>缩写</th>


                                    </tr>
                                    <tr  ng-click="queryDeviceZkValue(list, $event)" ng-repeat="list in items" >
                                        <td style="width:50px;" ng-bind="$index" ></td>
                                        <td ng-bind="list.TESTID"></td>


                                    </tr>
                                </table>
                            </div>
                        </td>
                        <td  valign="top">
                            <ul id="myTab2" class="nav nav-tabs">
                                <li class="active">
                                    <a href="#view-src-data" data-toggle="tab">
                                        质控原始数据
                                    </a>
                                </li>
                                <li>
                                    <a href="#view-zk-chart" data-toggle="tab">
                                        质控图
                                    </a>
                                </li>
                                <li>
                                    <a href="#view-sk-data" data-toggle="tab">
                                        失控数据
                                    </a>
                                </li>
                                <li>
                                    <a href="#view-sk-all-data" data-toggle="tab">
                                        所有结果
                                    </a>
                                </li>


                            </ul>
                            <div id="myTabContent" class="tab-content" style="overflow: auto;">
                                <div class="tab-pane fade in active" id="view-src-data" style="overflow: auto;width: 100%;height:100%">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th>序号</th>
                                            <th>日期</th>
                                            <th>结果</th>
                                            <th>差异量</th>
                                            <th>报告人</th>
                                            <th>质控品批号</th>
                                        </tr>
                                        <tr ng-repeat="item in results">
                                            <td ng-bind="$index">                                                    
                                            </td>

                                            <td ng-bind="item.ZK_DATE" >
                                            </td>
                                            <td ng-bind="item.RESULT" >
                                            </td>
                                            <td ng-bind="item.diff_amount" >
                                            </td>
                                            <td ng-bind="" >
                                            </td>
                                            <td ng-bind="item.BATCH_NUMBER" >
                                            </td>

                                        </tr>
                                    </table>
                                </div>
                                <div class="tab-pane " id="view-zk-chart" style="width: 100%;height: 100%;overflow: auto">
                                    <div class="box-shadow-8" style="width: 850px;height: 1150px;margin: 10px;padding: 10px;padding-right: 20px">
                                        <div style="width:100%;text-align: center;font-size: 28px;font-weight: bold;letter-spacing: 8px">
                                            <span ng-bind="hospitalName"></span><span>质控图</span>
                                        </div>
                                        <div style="width:100%;text-align: center;font-size: 16px;font-weight: 500;margin-top: 10px;">
                                            <span>仪器:</span><span ng-bind="device"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <span>项目:</span><span ng-bind="TESTID"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <span>月份:</span><span ng-bind="query_year"></span>年&nbsp;<span ng-bind="query_month"></span>月&nbsp;&nbsp;&nbsp;&nbsp;
                                            <span>批号:</span><span ng-bind="ph.BATCH_NUMBER"></span>&nbsp;
                                        </div>
                                        <div style="float:left;margin-left: 20px; font-size: 16px;">
                                            <div>
                                                <span>标准靶值:</span><span ng-bind="STANDARD_VALUE"></span>
                                            </div>
                                            <div>
                                                <span>标准SD值:</span><span ng-bind="SD"></span>
                                            </div>
                                            <div>
                                                <span>标准CV值:</span><span ng-bind="CV"></span>%
                                            </div>
                                        </div>
                                        <div style="float:right;font-size: 16px;" >
                                            <div>
                                                <span>本月平均值:</span><span ng-bind="ave"></span>
                                            </div>
                                            <div>
                                                <span>本月SD值:</span><span ng-bind="ave_sd"></span>
                                            </div>
                                            <div>
                                                <span>本月CV值:</span><span ng-bind="ave_cv"></span>
                                            </div>
                                            <div>
                                                <span>累计点数:</span><span ng-bind="result_size"></span>
                                            </div>

                                        </div>
                                        <div style="clear: both"></div>
                                        <div id="chart" style="max-height: 500px;height: 100%;max-width: 830px;width: 100%"></div>
                                        <div style="width: 100%">
                                            <div  ng-repeat="item in results" style="float: left;width: 33.3%;font-size: 16px;margin-top: 10px;padding-left: 15px;padding-right: 15px">

                                                <div  style=" float: left;width: 50%;border-bottom: 1px #888888 dashed"  ng-bind="item.ZK_DATE"></div>
                                                <div  style="float: left;width: 50%;border-bottom: 1px #888888 dashed" ng-bind="item.RESULT"></div>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane " id="view-sk-data">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th>批号</th>
                                            <th>项目</th>
                                            <th>日期</th>
                                            <th>结果</th>
                                            <th>符合失控的规则</th>
                                        </tr>
                                        <tr ng-if="item.SK" ng-repeat="item in results">
                                            <td ng-bind="ph.BATCH_NUMBER"></td>
                                            <td ng-bind="TESTID"></td>
                                            <td ng-bind="item.ZK_DATE"></td>
                                            <td ng-bind="item.RESULT"></td>
                                            <td ng-bind="item.SK"></td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="tab-pane " id="view-sk-all-data">
                                    <div class="form-inline">
                                        <div class="form-group">
                                            <label for="startTime">日期:</label>
                                            <input id="sample_date" ng-model="sample_date" style="width: 120px"  class="form-control" />
                                        </div>
                                        <button ng-click="saveEidtItem()" class="btn btn-success btn-sm" ><span class="fa fa-save"></span>保存修改</button>
                                    </div>
                                    <hr>
                                    <table class="table table-bordered">
                                        <tr>
                                        <th>序号</th>
                                        <th>项目</th>
                                        <th>时间</th>
                                        <th>结果</th>
                                        </tr>
                                        <tr ng-repeat="item in allItems">
                                            <td ng-bind="$index" >
                                                
                                            </td>
                                            <td ng-bind="item.TESTID"></td>
                                            <td ng-bind="item.ZK_DATE"></td>
                                            <td contenteditable="true" ng-model="item.RESULT"></td>
                                        </tr>
                                        </table>
                                </div>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>

            </div>

        </div>

    </body>
</html>
<script>
    angular.module("zkManagerApp", ['baseModule', 'ui.bootstrap'])

            .controller("mainCtrl", function ($scope, base) {
                $scope.query_year = new Date().getFullYear();
                $scope.query_month = new Date().getMonth() + 1;
                $scope.hospitalName = config.hospitalName;
                 $scope.sample_date = new Date().Format("yyyy-MM-dd")+"";
                console.log(DayNumOfMonth($scope.query_year, $scope.query_month));
                $scope.getDeviceType = function () {
                    base.post("${base}/view/host/getType").get(function (data) {
                        $scope.des = data;
                    });
                };
                $scope.getPh = function () {
                    $scope.items = [];
                    base.post("zk_value/getPh", {device: $scope.device}).get(function (data) {
                        $scope.phs = data;
                        $scope.ph = data[0];
                        $scope.getAllItems();
                        $scope.queryDeviceStandard();
                    })
                };
                $scope.queryDeviceStandard = function () {

                    base.post("zk_value/getDeviceStandard", {device: $scope.device, zk_reagent_pk: $scope.ph.PK}).get(function (data) {
                        $scope.items = [];
                        $scope.items = data;
                    });
                };
                $scope.queryByBtn = function () {
                    $scope.queryDeviceStandard();
                }

                $scope.queryDeviceZkValue = function (item, event) {
                    $(event.target).parent().siblings().removeClass("bg-success");
                    $(event.target).parent().addClass("bg-success");
                    $(event.target).removeClass("bg-success");
                    getZkValue(item);
                }
                var getZkValue = function (item) {
                    base.post("zk_manager/getDeviceZkValue", {ZK_REAGENT_PK: $scope.ph.PK, device: $scope.device, date: $scope.query_year + "-" + $scope.query_month, TESTID: item.TESTID}).get(function (data) {
                        console.log(data);
                         $('#chart').highcharts(zkChart(null, [], []));
                        if (data) {
                            checkResult(data);
                            $scope.STANDARD_VALUE = parseFloat(data[0].STANDARD_VALUE).toFixed(2);
                            $scope.SD = parseFloat(data[0].SD).toFixed(2);
                            $scope.CV = parseFloat(data[0].CV).toFixed(2);
                            $scope.TESTID = item.TESTID;
                            $scope.result_size = data.length;
                        }
                    });
                }
                var checkResult = function (datas) {
                    var total = 0.0;//
                    var total_sd = 0.0;
                    var diff_array = [];
                    for (var i = 0; i < datas.length; i++) {
                        total += parseFloat(datas[i].RESULT);
                        datas[i].diff_amount = (parseFloat(datas[i].RESULT) - parseFloat(datas[i].STANDARD_VALUE)).toFixed(4);
                        diff_array.push(checkChartResult(datas[i]));
                        // total_sd+=Math.sqrt(total);
                    }
                    $scope.ave = (total / datas.length).toFixed(2);//计算平均值

                    $scope.results = datas;
                    handlder_sd(datas);
                    changeChart(datas[0].SD, diff_array);
                }
                var checkChartResult = function (item) {
                    var newItem = {
                        "x": parseInt(item.ZK_DATE.split("-")[2]) - 1,
                        "y": ""
                    };
                    console.log(parseFloat(item.diff_amount) + "   " + (parseFloat(item.SD) * 4));
                    if (parseFloat(item.diff_amount) > (parseFloat(item.SD) * 4)) {
                        newItem.y = parseFloat(item.SD) * 4;
                    } else if (parseFloat(item.diff_amount) < (parseFloat("-" + item.SD) * 4)) {
                        newItem.y = parseFloat(-item.SD) * 4;
                    } else {
                        newItem.y = parseFloat(item.diff_amount);
                    }
                    return newItem;

                }

                //计算标准差
                var handlder_sd = function (datas) {
                    var sum = 0.0;
                    for (var i = 0; i < datas.length; i++) {
                        var dev = parseFloat(datas[i].RESULT) - parseFloat($scope.ave);
                        sum += Math.pow(dev, 2);
                    }

                    $scope.ave_sd = Math.sqrt(sum / datas.length).toFixed(2);
                    $scope.ave_cv = (parseFloat($scope.ave_sd) / parseFloat($scope.ave) * 100).toFixed(2) + "%";
                }

//        1₃s 一个质控结果超过平均数±3s，就判断失控，该规则主要对随机误差敏感。
//    *   2₂s 两个连续的质控结果同时超过平均数＋2s或平均数－2s，就判断失控，该规则对系统误差敏感。
//    *   R₄s 一个质控结果超过平均数＋2s，另一个质控结果超过平均数－2s，就判断失控，该规则对随机误差敏感。(同一天的两个质控结果)
//    *   4₁s 四个连续的质控结果同时超过平均数＋1s或平均数－1s，就判断失控，该规则对系统误差敏感。
//    *   10x 10个连续的质控结果落在平均数的一侧（高或低于平均数，对偏离的大小没有要求），就判断失控，该规则对系统误差敏感。
                var changeChart = function (sd, data) {
                    //console.log(data);return;
                    // 给失控点添加颜色
                    var newData = [];
                    for (var i = 0; i < data.length; i++) {
                        //newData.push({"y":data[i],"color":"#"+Math.floor(Math.random()*0xffffff).toString(16)});
                        var desc_ = "";
                        var color = "rgb(124, 181, 236)";
                        if (check_13s(sd, i, data)) {
                            color = "red";
                            desc_ += " 1₃s";
                        }
                        if (check_22s(sd, i, data)) {
                            color = "red";
                            desc_ += " 2₂s";
                        }
                        if (check_41s(sd, i, data)) {
                            color = "red";
                            desc_ += " 4₁s";
                        }
                        if (check_10x(sd, i, data)) {
                            color = "red";
                            desc_ += " 10x";
                        }
                        if (desc_ != "") {
                            $scope.results[i].SK = desc_;
                        }
                        newData.push({"y": data[i].y, "x": data[i].x, desc_: desc_, marker: {
                                fillColor: color, //数据点标志填充的颜色
                                //lineColor: "#FFFFFF",  //数据点标志线的颜色
                                //lineWidth: 0,          //数据点标志线的大小
                                radius: 4
                            }});
                    }
                    // 获取当月天数
                    var days = DayNumOfMonth($scope.query_year, $scope.query_month);
                    var day = [];
                    for (var s = 1; s < days + 1; s++) {
                        day.push(s);
                    }
                    $('#chart').highcharts(zkChart(sd, newData, day));
                }
                var check_13s = function (sd, i, data) {
                    if (Math.abs(parseFloat(data[i].y)) > parseFloat(sd) * 3) {
                        return true;
                    }
                }
                var check_22s = function (sd, i, data) {
                    var flag = false;
                    if (i - 1 >= 0) {//判断上一个结果是否存在
                        if (parseFloat(data[i].y) > 0 && parseFloat(data[i - 1].y) > 0) {//判断两个结果是否同为正数
                            if (parseFloat(data[i].y) > (parseFloat(sd) * 2) && parseFloat(data[i - 1].y) > (parseFloat(sd) * 2)) {
                                return true;
                            }
                        } else if (parseFloat(data[i].y) < 0 && parseFloat(data[i - 1].y) < 0) {//判断两个结果是否同为负数
                            if (parseFloat(data[i].y) < (parseFloat(sd) * 2 - parseFloat(sd) * 4) && parseFloat(data[i - 1].y) < (parseFloat(sd) * 2 - parseFloat(sd) * 4)) {
                                return true;
                            }
                        }
                    }
                }
                var check_R4s = function () {

                }
                var check_41s = function (sd, i, data) {
                    if (i - 3 >= 0) {
                        var s = parseFloat(data[i].y);
                        var s_1 = parseFloat(data[i - 1].y);
                        var s_2 = parseFloat(data[i - 2].y);
                        var s_3 = parseFloat(data[i - 3].y);
                        sd = parseFloat(sd);
                        if (s > 0 && s_1 > 0 && s_2 > 0 && s_3 > 0) {
                            if (s > sd && s_1 > sd && s_2 > sd && s_3 > sd) {
                                return true;
                            }
                        } else if (s < 0 && s_1 < 0 && s_2 < 0 && s_3 < 0) {
                            if (s < sd - 2 * sd && s_1 < sd - sd * 2 && s_2 < sd - sd * 2 && s_3 < sd - sd * 2) {
                                return true;
                            }
                        }
                    }
                }
                var check_10x = function (sd, i, data) {
                    if (i - 9 >= 0) {
                        if (parseFloat(data[i].y) > 0) {
                            for (var j = i; j > (i - 10); j--) {
                                if (parseFloat(data[j].y) < 0) {
                                    return false;
                                }
                            }
                            return true;
                        } else {
                            for (var j = i; j > (i - 10); j--) {
                                if (parseFloat(data[j].y) > 0) {
                                    return false;
                                }
                            }
                            return true;
                        }
                    }

                }
                $scope.printChart = function () {
                    $(".box-shadow-8").jqprint({
                        debug: true, //如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
                        importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
                        printContainer: true, //表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
                        operaSupport: true//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
                    });
                }
                
                
                $scope.getAllItems = function(){
                    base.post("zk_manager/getAllItems",{ZK_REAGENT_PK: $scope.ph.PK, device: $scope.device, date: $scope.sample_date}).get(function(data){
                        if(data){
                            $scope.allItems =data;
                        }else{
                            data=[];
                        }
                    });
                }
                $scope.saveEidtItem = function(){
                    base.post("zk_manager/saveItem",{data:angular.toJson($scope.allItems)}).get(function(data){
                        if(data=='true'){
                            steveAl("修改成功",1000);
                              $scope.getAllItems();
                        }
                        else{
                            steveAl("保存失败",1000);
                        }
                    })
                }
                
                  //日期
                layui.use('laydate', function () {
                    var laydate = layui.laydate;

                    var start = {
                        istoday: true
                        , choose: function (datas) {
                            $scope.sample_date = datas;
                           $scope.getAllItems();

                        }
                    };
                    $scope.sample_date = laydate.now();
                    //$scope.$apply();
                    document.getElementById('sample_date').onclick = function () {
                        start.elem = this;
                        laydate(start);
                    }


                });

                

                $scope.getDeviceType();
                




            });





    function autoHeight() {
        var bHeight = $(window).height();
        var bWidth = $(window).width();

        $(".main").height(bHeight - $(".head-width").height() - 75);
        $(".add-zk-object").height(bHeight - $(".head-width").height() - 145);
        $("#myTabContent").height(bHeight - $(".head-width").height() - 145);

    }
    autoHeight();


    $(window).resize(function () {
        autoHeight();
    });
</script>
